package com.itheima.web.manager;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.crypto.SecureUtil;
import com.itheima.domain.db.Admin;
import com.itheima.service.db.AdminService;
import com.itheima.service.db.UserInfoService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.vo.PageBeanVo;
import com.itheima.web.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Reference
    private AdminService adminService;

    @Reference
    private UserInfoService userInfoService;


    //生成验证码
    public LineCaptcha genVerification(String uuid) {

        LineCaptcha lineCaptcha = new LineCaptcha(300,150,5,500);
        String code = lineCaptcha.getCode();//获取验证码
        System.out.println(code);

        //将验证码封入Redis(设置有效期）
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE + uuid, code, Duration.ofMinutes(5));

        //返回验证码
        return lineCaptcha;

    }

    //用户登录
    public String login(String username, String password, String verificationCode, String uuid) {

        //从Redis中获取验证码
        String codeFromRedis = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);

        //核对验证码
        if (!StringUtils.equals(verificationCode, codeFromRedis)) {
            throw new BusinessException("验证码错误！");
        }

        //验证码核对成功，查询数据库
        Admin admin = adminService.findByUsername(username);

        if (admin == null) {
            throw new BusinessException("用户名错误或不存在！");
        }

        //对比密码是否正确
        String passwordMd5 = SecureUtil.md5(password);
        if (!StringUtils.equals(admin.getPassword(), passwordMd5)) {
            throw new BusinessException("密码错误！");
        }

        //用户登录成功
        //删除Redis中的验证码
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE + uuid);

        //生成token返回给前端
        Map<String, Object> map = new HashMap<>();
        map.put("id", admin.getId());
        map.put("username", username);
        String token = JwtUtil.createToken(map);

        //将生成的token存入Redis
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, "1", Duration.ofHours(1));
        //stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, admin.getId() + username, Duration.ofHours(1));

        //返回token
        return token;

    }



    //基本信息查询
    public Admin findUserInfo(String username) {

        return adminService.findByUsername(username);

    }



    //用户登出
    public void logOut(String token) {

        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN + token);

    }

}
